package game;

import java.io.File;
import java.io.IOException;

import system.Actor;
import system.AnimatedSprite;
import system.InputManager;

public class Player extends Actor{
	
	// States
	public final int STATE_DEFAULT = 0;
	public final int STATE_MOVINGUP = 1;
	public final int STATE_MOVINGDOWN = 2;
	public final int STATE_GETTINGHIT = 3;
	public final int STATE_DYING = 4;

	public final int SPEED = 5;
	
	private boolean releasedSpace = true;
	private boolean fireBullet;				// when set to true, game will fire a bullet
	private static int POWER = 25;          // damage of bullet
	
	private AnimatedSprite[] sprites;       // array of player sprites, arranged by state

	public Player(AnimatedSprite sprite, long x, long y) {
		super("Player", sprite, x, y, 0, 0, POWER);
		
		sprites = new AnimatedSprite[5];
		try {
			sprites[0] = new AnimatedSprite(new File("assets/sprites/ship.png"),58,27,true, 90);
			sprites[1] = new AnimatedSprite(new File("assets/sprites/ship-up.png"),54,44,false, 50);
			sprites[2] = new AnimatedSprite(new File("assets/sprites/ship-down.png"),54,44,false, 50);
			sprites[3] = new AnimatedSprite(new File("assets/sprites/ship.png"),35,19,true, 90);
			sprites[4] = new AnimatedSprite(new File("assets/sprites/ship-die.png"),189,184,false, 100);
		} catch (IOException e) {
			System.out.println("We couldn't load player sprites");
		}
		
		fireBullet = false;
	}

	public void update(InputManager inputs){
		
		if(myHp <= 0 && state != 4){
			setState(4); // die
			x -= 97;    // adjust for different sprite size
			y -= 90;
		}
		
		if(inputs.isPressing(inputs.KEY_UP)){
			y -= SPEED;
		}
		if(inputs.isPressing(inputs.KEY_DOWN)){
			y += SPEED;
		}
		if(inputs.isPressing(inputs.KEY_LEFT)){
			x -= SPEED;
		}
		if(inputs.isPressing(inputs.KEY_RIGHT)){
			x += SPEED;
		}

		if(inputs.isPressing(inputs.KEY_SPACE) && releasedSpace){
			fireBullet = true;
			releasedSpace = false;			
		}
		if(!inputs.isPressing(inputs.KEY_SPACE)){
			releasedSpace = true;
			inputs.handledKey(inputs.KEY_SPACE);
		}
		
		setSprite(sprites[state]);
		sprite.update();
		
			if(state!=4){
				
		
				if (x < 0){
					x = 0;
				} else if (x > 480 - this.getImage().getWidth()){
					x = 480 - this.getImage().getWidth();
				}	
				if (y < 0) {
					y = 0; 
				} else if (y > 225) {
					y = 225;
				}
			}
			else {
				
			}
	}

	public boolean getFireBullet(){
		return fireBullet;
	}

	public void setFireBullet(boolean fire){
		fireBullet = fire;
	}

}
